package com.szly.phm.admin.controller;

import com.szly.phm.admin.aspectj.annotation.OperationLogAnnotation;
import com.szly.phm.admin.aspectj.enums.ModuleEnum;
import com.szly.phm.admin.aspectj.enums.OperationActionEnum;
import com.szly.phm.admin.utils.CustomResponse;
import com.szly.phm.admin.utils.SecurityUtils;
import com.szly.phm.common.entity.dto.permission.CreatePermissionDTO;
import com.szly.phm.common.entity.dto.permission.PermissionDTO;
import com.szly.phm.common.entity.dto.permission.PermissionSortDTO;
import com.szly.phm.common.entity.dto.permission.UpdatePermissionDTO;
import com.szly.phm.common.entity.dto.user.LoginUserDTO;
import com.szly.phm.common.entity.result.PageResult;
import com.szly.phm.common.entity.result.RootResponse;
import com.szly.phm.common.entity.vo.permissiom.PermissionVO;
import com.szly.phm.common.service.ISysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-07-02
 */
@RestController
@Validated
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/pc/permission")
@Api(tags = "菜单管理")
public class SysPermissionController {

    private final ISysPermissionService permissionService;

    @ApiOperation(value = "分页查询菜单列表")
    @GetMapping("/v1/permissions")
    @CustomResponse
    public RootResponse<PageResult<PermissionVO>> queryPermissionPageV1(@Valid PermissionDTO permissionDTO) {
        return permissionService.queryPermissionPage(permissionDTO);
    }

//    @ApiOperation(value = "重置idPath")
//    @GetMapping("/v1/resetIdPath")
//    public RootResponse reset() {
//        return permissionService.resetIdPath();
//    }
    @ApiOperation(value = "查询权限树")
    @GetMapping("/v1/treePermission")
    @CustomResponse
    public RootResponse reset() {
        return permissionService.treePermission();
    }

    @ApiOperation(value = "添加权限")
    @OperationLogAnnotation(getModuleName = ModuleEnum.PERMISSION, getOperationAction = OperationActionEnum.SAVE)
    @PostMapping("/v1/permissions")
    @CustomResponse
    public RootResponse createPermissionV1(@Valid @RequestBody CreatePermissionDTO roleDTO) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return permissionService.createPermission(roleDTO, userDTO);
    }

    @ApiOperation(value = "修改权限")
    @OperationLogAnnotation(getModuleName = ModuleEnum.PERMISSION, getOperationAction = OperationActionEnum.UPDATE)
    @PutMapping("/v1/permissions")
    @CustomResponse
    public RootResponse updatePermissionV1(@Valid @RequestBody UpdatePermissionDTO roleDTO) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return permissionService.updatePermissin(roleDTO, userDTO);
    }

    @ApiOperation(value = "删除权限")
    @OperationLogAnnotation(getModuleName = ModuleEnum.PERMISSION, getOperationAction = OperationActionEnum.DELETE)
    @ApiImplicitParam(name = "id", value = "角色Id", dataType = "String", required = true, dataTypeClass = String.class)
    @DeleteMapping("/v1/permissions")
    @CustomResponse
    public RootResponse deletePermissionV1(@NotBlank(message = "角色Id不能为空") @RequestParam(value = "id") String id) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return permissionService.deletePermission(id, userDTO);
    }

    /**
     * @ Description   :  调整菜单顺序
     * @ Author        :  chengjiang
     * @ Date          :  2021/5/12
     */
    @ApiOperation(value = "修改权限顺序")
    @OperationLogAnnotation(getModuleName = ModuleEnum.PERMISSION, getOperationAction = OperationActionEnum.UPDATE)
    @ApiImplicitParam(name = "sorts", value = "id", allowMultiple = true, dataType = "PermissionSortDTO", dataTypeClass = PermissionSortDTO.class)
    @ApiResponse(code = 200, message = "", response = List.class)
    @PutMapping("/v1/changePermissionSort")
    @CustomResponse
    public RootResponse changePermissionSort(@RequestBody List<PermissionSortDTO> sorts) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return permissionService.changePermissionSort(sorts, userDTO);
    }
}

